package com.block.source.util;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.symmetric.AES;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: ld
 * @Date: 2024-08-26 11:25
 * @Description: AES加解密
 * @Version: 1.0
 **/
public class JiXiangYouQianAESUtils {




    public static String decode(String apiSecret, JSONObject jsonObject) {
        String aseKey = apiSecret.substring(0, 16);
        String signKey = apiSecret.substring(16);
        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, aseKey.getBytes());
        String result = aes.decryptStr(jsonObject.getString("encryptData"));
        String signature = jsonObject.getString("signData");
        String reSign = DigestUtil.sha1Hex(result + signKey);
        if (signature != null && signature.equals(reSign)) {
            return result;
        }
        return null;
    }

}
